import json
import os
import requests
import sys

import util

url = os.getenv('JIANMU_BOOT_WEBHOOK_URL')
secret = os.getenv('JIANMU_SECRET')
signs = util.get_signature(secret)
links_str = os.getenv('JIANMU_LINKS')
headers = {
    'Content-Type': 'application/json'
}
params = {
    'sign': signs.get('sign'),
    'timestamp': signs.get('timestamp'),
}
try:
    links = json.loads(links_str if links_str else '[]')
except Exception as e:
    print('error: 参数links格式错误，如：\'[{"title":"标题","messageURL":"https://jianmuhub.com/","picURL":"image.png"}]\'')
    sys.exit(1)
data = {
    'msgtype': 'feedCard',
    "feedCard": {
        "links": links
    }
}
response = requests.post(url=url, headers=headers, params=params, data=json.dumps(data))
content = response.json()
if content.get("errcode") != 0:
    raise Exception(content)
print(content)
